package com.txunda.zhuohired.aty.user.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.util.JSONUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.adapter.user.home.SelectCityAdapter;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.bean.user.home.CityBean;
import com.txunda.zhuohired.http.pst.MemberHomePst;
import com.txunda.zhuohired.util.PinyinUtils;
import com.txunda.zhuohired.view.SideBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/2/27 11:08
 * 功能描述：选择城市
 * 联系方式：jiahuanchen@126.com
 */

public class SelectCityAty extends BaseAty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_blue_center)
    private TextView tv_layout_blue_center;
    //    /**
    //     * 热门城市
    //     */
    //    @ViewInject(R.id.gv_select_city)
    //    private GridViewForScrollView gv_select_city;
    /**
     * 其他城市
     */
    @ViewInject(R.id.lv_select_city)
    private ListView lv_select_city;
    /**
     * sideBar
     */
    @ViewInject(R.id.side_select_city)
    private SideBar side_select_city;

    @ViewInject(R.id.tv_select_city)
    private TextView tv_select_city;

    @ViewInject(R.id.ll_select_city)
    private LinearLayout ll_select_city;
    @ViewInject(R.id.ll_no_data)
    private LinearLayout ll_no_data;

    private SelectCityAdapter selectCityAdapter;
    private MemberHomePst memberHomePst;
    private List<CityBean.DataBean> cityList;
    private String token = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_blue_head);
        tv_layout_blue_center.setText("选择城市");
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_select_city;
    }

    @Override
    protected void initialized() {
        token = application.getUserInfo().get("data");
        memberHomePst = new MemberHomePst(this);
        cityList = new ArrayList<>();
    }

    private void initCityAdapter() {
        cityList = filledData(cityList);
        selectCityAdapter = new SelectCityAdapter(this, cityList);
        lv_select_city.setAdapter(selectCityAdapter);
        lv_select_city.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                memberHomePst.cityIn(token, cityList.get(i).getRegion_name());

            }
        });
    }

    private void initData() {
        side_select_city.setTextView(tv_select_city);
    }

    private void initEvents() {
        //设置右侧触摸监听
        side_select_city.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                //该字母首次出现的位置
                int position = selectCityAdapter.getPositionForSection(s.charAt(0));
                if (position != -1) {
                    lv_select_city.setSelection(position + 1);
                }
            }
        });

        //ListView的点击事件
        lv_select_city.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
            }
        });

    }

    @Override
    protected void requestData() {
        memberHomePst.cityList();
        initData();
        //
    }

    private List<CityBean.DataBean> filledData(List<CityBean.DataBean> date) {
        List<CityBean.DataBean> mSortList = new ArrayList<>();
        ArrayList<String> indexString = new ArrayList<>();

        for (int i = 0; i < date.size(); i++) {
            CityBean.DataBean sortModel = date.get(i);
            String pinyin = PinyinUtils.getPingYin(date.get(i).getRegion_name());
            String sortString = pinyin.substring(0, 1).toUpperCase();
            if (sortString.matches("[A-Z]")) {
                sortModel.setSortLetters(sortString.toUpperCase());
                if (!indexString.contains(sortString)) {
                    indexString.add(sortString);
                }
            } else {
                sortModel.setSortLetters("#");
                if (!indexString.contains("#")) {
                    indexString.add("#");
                }
            }
            mSortList.add(sortModel);
        }
        Collections.sort(indexString);
        Collections.sort(mSortList);
        side_select_city.setIndexText(indexString);
        return mSortList;
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("cityList")) {
            //            L.e("===========cityList============", jsonStr);
            CityBean cityBean = GsonUtil.GsonToBean(jsonStr, CityBean.class);
            cityList = cityBean.getData();
            if (cityList.size() == 0) {
                noDate(1);
            } else {
                noDate(2);
                initEvents();
                initCityAdapter();
            }
            return;
        }
        if (requestUrl.contains("cityIn")) {
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            showRightTip(map.get("message"));
            //            Intent intent = new Intent();
            //            setResult(RESULT_OK, intent);
            finish();
            return;
        }
    }

    private void noDate(int type) {
        if (type == 1) {
            ll_select_city.setVisibility(View.GONE);
            ll_no_data.setVisibility(View.VISIBLE);
        } else {
            ll_no_data.setVisibility(View.GONE);
            ll_select_city.setVisibility(View.VISIBLE);
        }
    }
}
